package com.codergd.gra.service;

import com.codergd.gra.bean.User;
import com.codergd.gra.config.RestResult;
import com.codergd.gra.config.ResultGenerator;
import com.codergd.gra.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserDao userDao;

    @Autowired
    private ResultGenerator generator;

    public RestResult isLogin(String iphone, String paw){
        //用户不存在
        if (userDao.selectUser(iphone).size() == 0){
            return generator.getSuccessResult(-1);
        //用户密码错误
        }else if(!userDao.selectUser(iphone).get(0).passWord.equals(paw)){
            return generator.getSuccessResult(0);
        }
        //密码正确
        return generator.getSuccessResult(userDao.selectUser(iphone).get(0));
    }

    public int registered(User user){
        //用户已存在
        if (!isLogin(user.iphone,user.passWord).getData().toString().equals("-1")){
            return 0;
        }
        //数据插入成功
        if(userDao.insertUser(user) != -1) {
            return 1;
        }
        return -1;
    }

    //根据手机号查找id
    public int selectId(String iphone){
        return userDao.selectId(iphone);
    }

    //修改简历
    public int changeResume(int id,String resume){
        return userDao.changeResume(id,resume);
    }

    //修改头像
    public int changeHead(int id,String image){
        return userDao.changeHead(id,image);
    }

    //修改账号信息
    public int changeInfo(int id,String name,String iphone,String paw){
        return userDao.changeInfo(id, name, iphone, paw);
    }

    //修改意向
    public int changeWant(int id,String work,String city,int sal, String content,String work1,int sal1){
        return userDao.changeWant(id,work,city,sal,content,work1,sal1);
    }

    //根据id查看个人信息
    public User getUserInfo(int id){
        return userDao.selectUserInfo(id);
    }
}
